import vue from '@vitejs/plugin-vue';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';

const __dirname = dirname(fileURLToPath(import.meta.url));

// https://vite.dev/config/
export default defineConfig(({ mode }) => ({
  build: {
    lib: {
      // entry keys are mapped to generated filenames in dist directory.
      // For example: index will be index.js & index.cjs.
      entry: {
        index: resolve(__dirname, 'src/index.ts'),
        security: resolve(__dirname, 'src/security/index.ts'),
        storage: resolve(__dirname, 'src/storage/index.ts'),
      },
      formats: ['es'],
      name: 'MyNodeCore',
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ['jsonwebtoken', 'my-ts-core', 'vue'],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: 'Vue',
        },
        preserveModules: true, // Ensures file structure preserved in dist. Supports defineAsyncComponent.
        preserveModulesRoot: resolve(__dirname, 'src'), // Ensure correct module resolution. Supports defineAsyncComponent.
      },
    },
    minify: mode === 'debug' ? false : true, // Set to false to support webpack project debugging purpose ONLY.
  },
  plugins: [vue()],
  server: {
    proxy: {
      '/family-expense/api': 'http://localhost:8088',
    },
  },
}));
